﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WooCoo.Web
{
    /// <summary>
    /// 应用程序上下文管理
    /// </summary>
    public class ApplicationContextManager : IContextManager
    {
        private const string _localContextName = "WooCoo.LocalContext";
        private const string _clientContextName = "WooCoo.ClientContext";
        private const string _globalContextName = "WooCoo.GlobalContext";

        /// <summary>
        /// 指标管理器在当前环境中是否可用        
        /// </summary>
        public bool IsValid
        {
            get { return HttpContext.Current != null; }
        }

        /// <summary>
        /// 取得当前用户授权.
        /// </summary>
        public System.Security.Principal.IPrincipal GetUser()
        {
            return HttpContext.Current.User;
        }

        /// <summary>
        /// Sets the current principal.
        /// </summary>
        /// <param name="principal">Principal object.</param>
        public void SetUser(System.Security.Principal.IPrincipal principal)
        {
            HttpContext.Current.User = principal;
        }

        /// <summary>
        /// Gets the local context.
        /// </summary>
        public HybridDictionary GetLocalContext()
        {
            return (HybridDictionary)HttpContext.Current.Items[_localContextName];
        }

        /// <summary>
        /// Sets the local context.
        /// </summary>
        /// <param name="localContext">Local context.</param>
        public void SetLocalContext(HybridDictionary localContext)
        {
            HttpContext.Current.Items[_localContextName] = localContext;
        }

        /// <summary>
        /// Gets the client context.
        /// </summary>
        public HybridDictionary GetClientContext()
        {
            return (HybridDictionary)HttpContext.Current.Items[_clientContextName];
        }

        /// <summary>
        /// Sets the client context.
        /// </summary>
        /// <param name="clientContext">Client context.</param>
        public void SetClientContext(HybridDictionary clientContext)
        {
            HttpContext.Current.Items[_clientContextName] = clientContext;
        }

        /// <summary>
        /// Gets the global context.
        /// </summary>
        public HybridDictionary GetGlobalContext()
        {
            return (HybridDictionary)HttpContext.Current.Items[_globalContextName];
        }

        /// <summary>
        /// Sets the global context.
        /// </summary>
        /// <param name="globalContext">Global context.</param>
        public void SetGlobalContext(HybridDictionary globalContext)
        {
            HttpContext.Current.Items[_globalContextName] = globalContext;
        }


        public string SessionID
        {
            get
            {
                return (string)HttpContext.Current.Items["WooCoo.Web.SessionID"];
            }
            set
            {
                HttpContext.Current.Items["WooCoo.Web.SessionID"] = value;
            }
        }
    }
}
